package com.gbq.controller;

import com.gbq.service.BaseService;
import com.gbq.service.RedisService;
import lombok.extern.slf4j.Slf4j;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
public class AppController {


    @Autowired
    private BaseService baseService;

    @Autowired
    private RedisService redisService;


    @RequestMapping("/test")
    @ResponseBody
    public Object test(@RequestParam String data){

        return "";

    }

    @RequestMapping("/reducePerson")
    @ResponseBody
    public String reduce(@RequestBody JSONObject data){

        JSONArray arraysTemp = JSONArray.fromObject(data.get("data"));
        JSONArray arrays = new JSONArray();
        //将数据List获取到本地list中，只取cardNo
        arraysTemp.stream().forEach(array->{

            JSONObject objtemp = null;
            JSONObject obj = (JSONObject) array;
            String cardNo = obj.getString("cardNo");

            //先查Redis再查Oracle
            objtemp=redisService.selectPersonByCardno(cardNo);
            objtemp= objtemp.isNullObject()?objtemp:baseService.selectPersonByCardno(cardNo);


            log.info(objtemp.toString());
            //判断是否命中数据并增加相应信息
            if (!objtemp.isNullObject()){
                obj.put("warntype",objtemp.get("warntype"));
                obj.put("warninfo",objtemp.get("warninfo"));
                obj.put("warncode",objtemp.get("warncode"));
                arrays.add(obj);
            }else {
                obj.put("warntype","");
                obj.put("warninfo","");
                obj.put("warncode",0);
                arrays.add(obj);
            }
        });
        log.info("success");
        return arrays.toString();

    }

}
